<?php

namespace app\controller;

use app\model\SkiingPark;
use GuzzleHttp\Client;
use think\Controller;
use think\Request;

class Index extends Controller
{
    public function index()
    {
        return view('index');
    }


    public function pic(Request $request)
    {
        switch ($request->name) {
            case 'skiingpark':
                $picArr = ['prefix' => 'skiingpark', 'num' => 20];
                break;
            case 'material':
                $picArr = ['prefix' => 'material', 'num' => 6];
                break;
            case 'catering':
                $picArr = ['prefix' => 'catering', 'num' => 19];
                break;
            case 'catering':
                $picArr = ['prefix' => 'catering', 'num' => 20];
                break;
            default:
                $picArr = ['prefix' => 'skiingpark', 'num' => 20];
        }
        return view('pic', ['pic' => $picArr]);
    }

    public function reserve(Request $request)
    {
        $name = $request->name;
        if ($name) {
            $list = SkiingPark::whereLike('name', '%' . $name . '%')->select();
        } else {
            $list = SkiingPark::all();
        }
        $arr = $list->toArray();
        foreach ($arr as &$v) {
            $v['w']   = [
                'icon'     => '/images/sun.png',
                'desc'     => '',
                'temp'     => '0',
                'clouds'   => '0',
                'humidity' => '0',
                'ws'       => '0',
                'pressure' => '0',
                'sunrise'  => '',
                'sunset'   => ''
            ];
            $v['pic'] = explode(',', $v['pic'])[0];
        }
        return view('reserve', ['list' => $arr]);
    }

    public function getWeather(Request $request)
    {
        $arr = [
            'icon'     => '/images/sun.png',
            'desc'     => '',
            'temp'     => '0',
            'clouds'   => '0',
            'humidity' => '0',
            'ws'       => '0',
            'pressure' => '0',
            'sunrise'  => '',
            'sunset'   => ''
        ];
        $this->result($arr, 1, 'ok', 'json');
        $client          = new Client;
        $city            = $request->city;
        $response        = $client->get("http://api.openweathermap.org/data/2.5/weather?q=" . $city . "&units=metric&appid=ebcf5230b3446f334fe3fa2fd2d4ce24");
        $data            = json_decode($response->getBody(), true);
        $arr['icon']     = "http://openweathermap.org/img/w/" . $data['weather'][0]['icon'] . ".png";
        $arr['desc']     = $data['weather'][0]['description'];
        $arr['temp']     = $data['main']['temp'];
        $arr['clouds']   = $data['clouds']['all'];
        $arr['humidity'] = $data['main']['humidity'];
        $arr['ws']       = $data['wind']['speed'];
        $arr['pressure'] = $data['main']['pressure'];
        $arr['sunrise']  = date('h:i A', $data['sys']['sunrise']);
        $arr['sunset']   = date('h:i A', $data['sys']['sunset']);
        $this->result($arr, 1, 'ok', 'json');
    }

    public function service(Request $request)
    {
        $sid = $request->sid;
        if ($sid) {
            $one = SkiingPark::with('activitys,materials,hotels,caterings')->find($sid);
        } else {
            $this->error('param error');
        }
        return view('service', ['one' => $one]);
    }

}
